/**
 * 定义一个学生数组
 * 学生：编号、名字、性别、班级
 */

/**
 * 设计一个接口，
 * 我自己定义一种数据类型
 */
interface IStu {
    id: number,
    name: string,
    gender: string,
    classes: IClasses,
    phone?: string
}
interface IClasses {
    cid: number,
    name: string
}

let students: IStu[] = [
    {
        id: 1, name: "杜欣", gender: "女", phone: "344", classes: {
            cid: 1, name: "web35"
        }
    },
    {
        id: 2, name: "袁令", gender: "男", classes: {
            cid: 2, name: "web34"
        }
    },
    {
        id: 3, name: "童瑶", gender: "女", classes: {
            cid: 3, name: "web33"
        }
    }
]

function getValue(students: IStu[], search: string) {
    let stu = students.find(item => item.name == search)
    return stu
}

const result = getValue(students, "童瑶")

//?.是一种新的运算符，ES2020这个版本推出来。 可选链运算符
// result是undefined或者null的时候 不会.id
console.log(result?.id);

/**
 * 获取学生电话
 */
function getPhone(students: IStu[], id: number) {
    const stu = students.find(item => item.id == id)
    return stu?.phone
}

getPhone(students,1)


export { }